﻿using UnityEngine;
using System.Collections;

public class SensorAlphamap : Sensor {

	public Terrain inputTerrain;
	public int inputTerrainTextureIndex;
	public float inputTerrainSize;

	public int x;
	public int y;
	public float a, b;
	public float fx, fz;
	public int probexs, probeys;

	void FixedUpdate () {
		probexs = inputTerrain.terrainData.alphamapWidth;
		probeys = inputTerrain.terrainData.alphamapHeight;
		fx = transform.position.x;
		fz = transform.position.z;
		fx = (fx - inputTerrain.transform.position.x) / inputTerrainSize;
		fz = (fz - inputTerrain.transform.position.z) / inputTerrainSize;
		fx = Mathf.Clamp01 (fx);
		fz = Mathf.Clamp01 (fz);
		x = (int)(fx * (probexs-1));
		y = (int)(fz * (probeys-1));
		float[,,] map = inputTerrain.terrainData.GetAlphamaps (x, y, 1, 1);
		a = map [0, 0, 0];
		b = map [0, 0, 1];

		output = (ushort)(65534.0f* map [0, 0, inputTerrainTextureIndex]);
	}
}
